package com.anzia.vueblog.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 *@ClassName CorsConfig
 *@Description
 *@Author Anzia.
 *@Date 2021/4/24 21:11
 *@Version 1.0
 **/
// 解决跨域问题
@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }
    //@Bean
    //public FilterRegistrationBean corsFilter() {
    //    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    //    CorsConfiguration config = new CorsConfiguration();
    //    config.setAllowCredentials(true);
    //    //config.addAllowedOrigin("http://localhost:8080/login");
    //    config.addAllowedOrigin("null");
    //    config.addAllowedHeader("*");
    //    config.addAllowedMethod("*");
    //    source.registerCorsConfiguration("/**", config);
    //    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    //    bean.setOrder(0);
    //    return bean;
    //}
}
